#include "stdafx.h"

#ifndef COLLABRC_H
#define COLLABRC_H

#include <QtGui/QMainWindow>
#include "ui_LoginForm.h"
#include "ui_collabrc.h"
#include "CollaborationChannel.h"
#include "RecentChangesChannel.h"
#include "LoggedEvent.h"
#include "RecentChange.h"
#include "ActionQueue.h"

class CollabRC : public QMainWindow
{
	Q_OBJECT

public:
	CollabRC(QWidget *parent = 0, Qt::WFlags flags = 0);
	~CollabRC();

public slots:
	void AddEvent(CLoggedEvent* event);
	void UpdateEvent(CLoggedEvent* event);
	void EventDestroyed(QObject* event);
	void RecentChange(QString page, QString user, unsigned int diff, int change);
	void ChangeFlagged(unsigned int diff, float probability, QString bot, QString remarks);
	void ChangeAccepted(CRecentChange* changeItem);
	void RecentChangeSelected();

signals:
	void LogUpdated();
	void AcceptedChange(CRecentChange* change);
public slots:
	void Pass();
	void Rollback();
	void Warn();
	void LoginCancelled();
	void CollaborationConnectionEstablished();
	void UpdateCollaborationUsers(unsigned int nBots, unsigned int nClients);
	
private:
	bool m_loginComplete;

	Ui_MainWindow ui;
	Ui_LoginForm login;

	CCollaborationChannel m_collaboration;
	CRecentChangesChannel m_rc;
	CActionQueue m_queue;

	CRecentChange *m_currentChange;

	static const int MAX_PENDING_CHANGES = 10;
	static const int FLAGGED_CHANGE_FACTOR = 2;

	struct EventData 
	{
		time_t timestamp;
		CLoggedEvent::EventType type;
		QString name;
		QString details;
	};

	QList<EventData> m_log;
	QHash<int, int> m_logDictionary;
	QStandardItemModel m_logModel;
	static QNetworkAccessManager manager;

	QHash<unsigned int, CRecentChange*> m_changeList;

	static QStandardItem* BuildMessageType(CLoggedEvent* event);

	QString GetIrcName();
	QString GetRcChannel();
	void CollectLoginDetails();
	QString GetNetworkCode();

	void SelectNext();
public:
	static QNetworkAccessManager& GetNetworkManager() { return manager; };
	static QNetworkRequest CreateNetworkRequest(const QUrl& url );
	static QString GetSummaryTag()
	{
		QString tag = "([[User:Shirik/CollabRC|CRC]]";
#ifdef _DEBUG
		tag += "-d";
#endif
		tag += ")";
		return tag;
	}
};

#endif // COLLABRC_H
